package com.css.fxfzaqbz.modules.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.base.entity.HouseData;
import com.css.fxfzaqbz.modules.base.queryParams.SeismicQueryParam;
import com.css.fxfzaqbz.modules.base.service.SeismicService;
import com.css.fxfzaqbz.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 房屋抗震能力评估
 */
@RestController
@RequestMapping("/fxfzaqbz/seismic")
@Slf4j
public class SeismicController {
    @Resource
    SeismicService seismicService;
    @Resource
    RestTemplateUtil restTemplateUtil;

    /*
    * 抗震能力算法 评估
    * */
    @PostMapping("/countSeismic")
    public RestResponse countSeismic(@RequestBody SeismicQueryParam param) {
        try {
            return seismicService.countSeismic(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.fail("业务繁忙,请稍后再试");
    }


    /*
    *
    * 房屋破坏算法 评估
    * */
    @PostMapping("/houseDestory")
    public RestResponse houseDestory(@RequestBody SeismicQueryParam param) {
        try {
            return seismicService.houseDestory(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.fail("业务繁忙,请稍后再试");
    }





    @GetMapping("/test")
    public List<HouseData> test(){
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("restTemplateUrl") + "/datacenter/house/getHouseData";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("province", "6");
        paramMap.put("city", "7");
        paramMap.put("county", "8");
        ResponseEntity<String> cchResponseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        List<HouseData> houses = JSONObject.parseArray(cchResponseEntity.getBody(), HouseData.class);
        return houses;
    }
}